package sjtu.ist.warehouse4ws.action;

import java.util.Map;

import sjtu.ist.warehouse4ws.model.User;
import sjtu.ist.warehouse4ws.service.UserService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyPasswordAction extends ActionSupport {
	private User user;
	private String oldpwd;
	private String newpwd1;
	private String newpwd2;
	private String message;
	private boolean success;
	
	public String getOldpwd() {
		return oldpwd;
	}
	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}
	public String getNewpwd1() {
		return newpwd1;
	}
	public void setNewpwd1(String newpwd1) {
		this.newpwd1 = newpwd1;
	}
	public String getNewpwd2() {
		return newpwd2;
	}
	public void setNewpwd2(String newpwd2) {
		this.newpwd2 = newpwd2;
	}
	
	@JSON(name = "message")
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	private UserService userService;
	
	
	public String  execute() throws Exception{
		String str1="旧密码输入不对，请重新输入";
		String str2="两次输入新密码不一致";
		String str3="密码修改成功";
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();
		
		User user = (User)session.get("user");
		String userPassword = user.getPassword();
		if(!oldpwd.equals(userPassword)){
			setMessage(str1);
			return SUCCESS;
		}
		if(!newpwd1.equals(newpwd2)){
			setMessage(str2);
			return SUCCESS;
		}
		user.setPassword(newpwd1);
		userService.update(user);
		setMessage(str3);
		success=true;
		return SUCCESS;
	}

}
